import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this, // 让程序和设备的刷新频率一致
      duration: const Duration(milliseconds: 1000),
    )..repeat(reverse: true);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.refresh),
          onPressed: () {
            _controller.repeat(reverse: true); // 重复
          },
        ),
        appBar: AppBar(
          title: const Text('AnimatedIcon'),
        ),
        body: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AnimatedIcon(
                  icon: AnimatedIcons.close_menu,
                  progress: _controller,
                  size: 40,
                ),
                AnimatedIcon(
                  icon: AnimatedIcons.view_list,
                  progress: _controller,
                  size: 40,
                ),
                AnimatedIcon(
                  icon: AnimatedIcons.search_ellipsis,
                  progress: _controller,
                  size: 40,
                )
              ],
            ),
            const SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    _controller.forward(); // 运动一次
                  },
                  child: const Text('forward'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _controller.reverse();
                  },
                  child: const Text('reverse'), // 翻转
                ),
                ElevatedButton(
                  onPressed: () {
                    _controller.stop();
                  },
                  child: const Text('stop'), // 停止
                ),
                ElevatedButton(
                  onPressed: () {
                    _controller.reset();
                  },
                  child: const Text('reset'), // 重置
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
